package Socket;

import org.json.simple.JSONObject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.Stack;

public class Client {
    public static Socket socket;
    public static ObjectOutputStream oom;
    public static ObjectInputStream oim;
    public static String name="李文丰";
    public static String to="王鱼";
    public static boolean state=false;

    public static void main(String[] args) {
        connect();
    }
    public static void connect(){
        try {
            socket=new Socket("127.0.0.1",9999);
            oom=new ObjectOutputStream(socket.getOutputStream());
            oim=new ObjectInputStream(socket.getInputStream());
            new Thread(new Cliend_send()).start();
            new Thread(new Cliend_listen()).start();
            new Thread(new Cliend_heart()).start();
            System.out.println("输入");
            state=true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void reconnect(){
        while (!state)
        {
            connect();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(){
        try {
            oim.close();
            oom.close();
            socket.close();
            state=false;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
class Cliend_send implements Runnable{
    @Override
    public void run() {
        try {
            while (true){
                Scanner scanner=new Scanner(System.in);
                System.out.println("请输入消息");
                String msg=scanner.next();
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("type","chat");
                jsonObject.put("to",Client.to);
                jsonObject.put("from",Client.name);
                jsonObject.put("msg",msg);
                Client.oom.writeObject(jsonObject);
                Client.oom.flush();
            }
        }catch (Exception e){
            Client.close();
            e.printStackTrace();
        }
    }
}
class Cliend_listen implements Runnable{
    @Override
    public void run() {
        try {
            while (true){
                JSONObject jsonObject= (JSONObject) Client.oim.readObject();
                String from= (String) jsonObject.get("from");
                String msg=(String)jsonObject.get("msg");
                System.out.println(from+":"+msg);
            }
        }catch (Exception e){
            e.printStackTrace();
            Client.close();
        }
    }
}
class Cliend_heart implements Runnable{
    @Override
    public void run() {
        try {
            while (true){
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("type","heart");
                jsonObject.put("name","李文丰");
                Client.oom.writeObject(jsonObject);
                Client.oom.flush();
                Thread.sleep(5000);
            }
        }catch (Exception e){
            e.printStackTrace();
            Client.close();
            Client.reconnect();
        }
    }
}
